package com.woniuxy.joylifemarket.controller;

import com.woniuxy.common.entity.MarketAttention;
import com.woniuxy.joylifemarket.service.MarketAttentionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AttentionController {
    @Autowired
    MarketAttentionService marketAttentionService;

    //搜索用户的关注
    @RequestMapping("/inituserattention")
    public Object userAttention(String userid) {
        Map<String, Object> result = new HashMap<>();
        try {
//            System.out.println(userid);
            List<MarketAttention> allAttentions = marketAttentionService.findAllAttentionByUserId(userid);
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data", allAttentions);
//            System.out.println("allAttentions"+allAttentions);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }

    //搜索用户的所有粉丝
    @RequestMapping("/inituserfans")
    public Object userFans(String userid) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<MarketAttention> allFans = marketAttentionService.findAllFansByReceiveUserId(userid);
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data", allFans);
//            System.out.println("allFans"+allFans);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }

    //查询登录用户是否关注用户的状态
    @RequestMapping("/initattentionstatus")
    public Object collectStatus(String userid,String loginuserid) {
//        System.out.println("userid"+userid+"loginuserid"+loginuserid);
        Map<String, Object> result = new HashMap<>();
        try {
            Boolean attentionStatus = marketAttentionService.findAttentionStatus(userid, loginuserid);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data",attentionStatus);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }

    //更改登录用户关注用户的状态
    @RequestMapping("/changeattentionstatus")
    public Object changeAttentionStatus(String userId,String loginUserid,String attentionStatus) {
        System.out.println("userId:"+userId+",loginuserid:"+loginUserid+",attentionStatus:"+attentionStatus);
        Map<String, Object> result = new HashMap<>();
        try {
            //判断状态，如果没有关注，添加关注
            if (attentionStatus.equals("false")) {
                int i = marketAttentionService.insertAttention(userId,loginUserid);
                if (i!=0) {
                    result.put("code", 200);
                    result.put("msg", "success");
                    result.put("data",true);
                    return result;
                }else{
                    result.put("code", 500);
                    result.put("msg", "default");
                    return result;
                }
                //已经关注过，则执行取消收藏操作
            }else if(attentionStatus.equals("true")){
                int i = marketAttentionService.deleteAttention(userId, loginUserid);
                if (i!=0) {
                    result.put("code", 200);
                    result.put("msg", "success");
                    result.put("data",false);
                    return result;
                }else{
                    result.put("code", 500);
                    result.put("msg", "default");
                    return result;
                }
            }
            result.put("code", 500);
            result.put("msg", "default");
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }
}
